﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
namespace ToolCalendar.Helper
{
    public class Tools
    {
        #region Post请求

        /// <summary>
        /// Post提交数据
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="data">请求数据</param>
        /// <param name="flag">是否成功</param>
        /// <returns></returns>
        public static string Post(string url, string data, ref bool flag)
        {
            string result = string.Empty;
            try
            {
                if (!url.StartsWith("http://"))
                {
                    return "";
                }

                byte[] byteArray = Encoding.Default.GetBytes(data); //转化
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(url));
                webReq.Method = "POST";
                webReq.ContentType = "application/x-www-form-urlencoded";
                webReq.Timeout = 1000 * 60 * 5;
                webReq.ProtocolVersion = HttpVersion.Version11;
                ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

                webReq.ContentLength = byteArray.Length;
                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                result = sr.ReadToEnd();
                sr.Close();
                response.Close();
                newStream.Close();
                flag = true;
            }
            catch (Exception ex)
            {
                result = ex.Message;
                flag = false;
            }
            return result;
        }
        #endregion

        #region Get请求

        /// <summary>
        /// Get
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="Timeout">超时 毫秒 默认5分钟</param>
        /// <returns></returns>
        public static string Get(string url, int Timeout = 1000 * 60 * 5)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "GET";
                request.ContentType = "text/html;charset=UTF-8";
                request.UserAgent = null;
                request.Timeout = Timeout;

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();

                return retString;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }
        #endregion
        
        #region Cmd

        /// <summary>
        /// 执行CMD命令
        /// </summary>
        /// <param name="str">命令</param>
        public static void ExecCmd(string str)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();

            p.StandardInput.WriteLine(str);
            p.StandardInput.WriteLine("exit");
        }

        #endregion
    }
}